//首页加载完毕后发起一个ajax请求，拿到树的第一级目录
$(document).ready(function(){
	chinds = $.ajax({url:"/index.php/CgiClass/getChild?id=0",async:false});	
	if(chinds.responseText.length > 0){
		$("#cgitree").html(chinds.responseText);	
	}
});

//异步请求树的孩子节点
function getChild(e){
	var ui_id = $(e.target).parent("div").children("div").attr("id");
	if ($("#"+ui_id).text().length==0){
		var parentid = ui_id.split("-")[1];
		htmlobj=$.ajax({url:"/index.php/CgiClass/getChild?id="+parentid,async:false});	
		if(htmlobj.responseText.length > 0){
			$("#"+ui_id).html(htmlobj.responseText);
			$("#"+ui_id).css("margin-left","15px");
			changeIcan(e);
		}else {
			//如果该节点下没有内容可展示，这显示这个提示信息
			if($(e.target).parent("div").children(".error_msg").text().length == 0){
				$(e.target).parent("div").children(".error_msg").html("☻ 亲，她一无所有！");
			}else{
				$(e.target).parent("div").children(".error_msg").html("");
			}
		}
	}else{
		$("#"+ui_id).toggle();
		changeIcan(e);
	}
}

//展开和收起树的图标切换
function changeIcan(e){
	if($(e.target).css("background-image").indexOf("co.gif")>0){
		$(e.target).css("background-image","url(/httptest/tpl/default/Public/imgcache/imgs/ex.gif)");
		$(e.target).css("margin-top","5px");	
	}else{
		$(e.target).css("background-image","url(/httptest/tpl/default/Public/imgcache/imgs/co.gif)");
		$(e.target).css("margin-top","0px");	
		$(e.target).parent("div").children(".error_msg").html("");
	}
}

//function changeLinkColorOnmouseover(e){
//	$(e.target).css("color","#D35671");
//}
//function changeLinkColorOnmouseout(e){
//	$(e.target).css("color","");
//}

function changeLinkColorOnfocus(e){
	$(e.target).css("color","#D35671");
}

$(document).ready(function(){
	$("#cgitree").on('click',".ksmLeft",getChild);
//	$("a").on('mouseover',changeLinkColorOnmouseover);
//	$("a").on('mouseout',changeLinkColorOnmouseout);
	$("a").on('click',changeLinkColorOnfocus);
});
